package com.zwface.demo.greendao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.zwface.demo.model.db.JoinUserWithCourse;

import com.zwface.demo.model.db.Course;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "COURSE".
*/
public class CourseDao extends AbstractDao<Course, Long> {

    public static final String TABLENAME = "COURSE";

    /**
     * Properties of entity Course.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property CourseName = new Property(1, String.class, "courseName", false, "COURSE_NAME");
        public final static Property Section = new Property(2, int.class, "section", false, "SECTION");
        public final static Property SectionSpan = new Property(3, int.class, "sectionSpan", false, "SECTION_SPAN");
        public final static Property Week = new Property(4, int.class, "week", false, "WEEK");
        public final static Property ClassRoom = new Property(5, String.class, "classRoom", false, "CLASS_ROOM");
        public final static Property CourseFlag = new Property(6, int.class, "courseFlag", false, "COURSE_FLAG");
    }

    private DaoSession daoSession;

    private Query<Course> userInfo_CoursesQuery;

    public CourseDao(DaoConfig config) {
        super(config);
    }
    
    public CourseDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"COURSE\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"COURSE_NAME\" TEXT NOT NULL ," + // 1: courseName
                "\"SECTION\" INTEGER NOT NULL ," + // 2: section
                "\"SECTION_SPAN\" INTEGER NOT NULL ," + // 3: sectionSpan
                "\"WEEK\" INTEGER NOT NULL ," + // 4: week
                "\"CLASS_ROOM\" TEXT," + // 5: classRoom
                "\"COURSE_FLAG\" INTEGER NOT NULL );"); // 6: courseFlag
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"COURSE\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Course entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, entity.getCourseName());
        stmt.bindLong(3, entity.getSection());
        stmt.bindLong(4, entity.getSectionSpan());
        stmt.bindLong(5, entity.getWeek());
 
        String classRoom = entity.getClassRoom();
        if (classRoom != null) {
            stmt.bindString(6, classRoom);
        }
        stmt.bindLong(7, entity.getCourseFlag());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Course entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, entity.getCourseName());
        stmt.bindLong(3, entity.getSection());
        stmt.bindLong(4, entity.getSectionSpan());
        stmt.bindLong(5, entity.getWeek());
 
        String classRoom = entity.getClassRoom();
        if (classRoom != null) {
            stmt.bindString(6, classRoom);
        }
        stmt.bindLong(7, entity.getCourseFlag());
    }

    @Override
    protected final void attachEntity(Course entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public Course readEntity(Cursor cursor, int offset) {
        Course entity = new Course( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getString(offset + 1), // courseName
            cursor.getInt(offset + 2), // section
            cursor.getInt(offset + 3), // sectionSpan
            cursor.getInt(offset + 4), // week
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // classRoom
            cursor.getInt(offset + 6) // courseFlag
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, Course entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setCourseName(cursor.getString(offset + 1));
        entity.setSection(cursor.getInt(offset + 2));
        entity.setSectionSpan(cursor.getInt(offset + 3));
        entity.setWeek(cursor.getInt(offset + 4));
        entity.setClassRoom(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setCourseFlag(cursor.getInt(offset + 6));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(Course entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(Course entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Course entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "courses" to-many relationship of UserInfo. */
    public List<Course> _queryUserInfo_Courses(Long uId) {
        synchronized (this) {
            if (userInfo_CoursesQuery == null) {
                QueryBuilder<Course> queryBuilder = queryBuilder();
                queryBuilder.join(JoinUserWithCourse.class, JoinUserWithCourseDao.Properties.CId)
                    .where(JoinUserWithCourseDao.Properties.UId.eq(uId));
                userInfo_CoursesQuery = queryBuilder.build();
            }
        }
        Query<Course> query = userInfo_CoursesQuery.forCurrentThread();
        query.setParameter(0, uId);
        return query.list();
    }

}
